package com.bdqn.bank_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.bank_system.pojo.Account;
import com.bdqn.bank_system.pojo.TransactionRecord;
import com.bdqn.bank_system.service.AccountService;
import com.bdqn.bank_system.service.TransactionRecordService;
import com.bdqn.bank_system.utils.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author cuishujian
 * @date 2024/10/31
 */
@Controller
public class IndexController {

    @Resource
    private AccountService accountService;
    @Resource
    private TransactionRecordService transactionRecordService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/toErrorLogin")
    public String toErrorLogin(Model model){
        model.addAttribute("msg","账号或密码错误");
        return "login";
    }

    @RequestMapping("/main")
    public String toMain(){
        return "main";
    }

    @RequestMapping("/doLogin")
    public String doLogin(String cardno, String password, HttpSession session, Model model){
        if (cardno == null || cardno.equals("") || password == null || password.equals("")) {
            model.addAttribute("msg","请输入账号和密码");
            return "login";
        }
        Account loginAccount = accountService.login(cardno, password);
        if (loginAccount != null){
            session.setAttribute("loginAccount",loginAccount);
//            model.addAttribute("loginAccount",loginAccount);
            return "redirect:/main";
        }else{
            return "redirect:/toErrorLogin";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginAccount");
        session.invalidate();
        return "redirect:/toLogin";
    }

    @RequestMapping("/toDeal")
    public String toDeal(){
        return "deal";
    }


    @ResponseBody
    @RequestMapping("/deal")
    public String deal(String cardno, Double amount, HttpSession session){
        Account toAccount = (Account) session.getAttribute("loginAccount");
        int deal = transactionRecordService.deal(toAccount.getCardno(), cardno, amount);
        return deal > 0 ? "true" : "false";
    }

    @RequestMapping("/list")
    public String list(String openDate, String closeDate, @RequestParam(defaultValue = "1",required = false) Integer pageIndex, Model model){
        Page<TransactionRecord> page = new Page<>();

        page.setPageSize(4);
        page.setTotalCount(transactionRecordService.countDate(openDate,closeDate));
        page.setPageNo(pageIndex);
        page.setData(transactionRecordService.selectPageRecord(openDate,closeDate,page));

        model.addAttribute("page",page);
        model.addAttribute("openDate",openDate);
        model.addAttribute("closeDate",closeDate);
        return "list";
    }
}
